package goodenglish.domain.model.subt;

import goodenglish.domain.model.lexicon.Lexicon;
import goodenglish.domain.model.lexicon.NewWord;
import goodenglish.domain.service.SubtInType;
import goodenglish.domain.service.analysis.Analyzer;
import goodenglish.domain.service.analysis.ReBackAnalyzer;
import goodenglish.domain.service.analysis.SrtAnalyzer;
import goodenglish.domain.service.analysis.TxtAnalyzer;
import goodenglish.domain.service.schedule.ScheduleSubject;
import goodenglish.interfaces.UUID;

import java.io.File;

/**
 * 
 * @author qiuriyuchen E-mail:panpan_001@126.com
 */
public class SubtFactory {

	/**
	 * 根据要导入的文件创建字幕对象
	 * 
	 * @param file
	 * @return
	 */
	public Subt createSubt(final File file, final SubtInType subtInType) {

		Analyzer analyzer = createAnalyzer(file, subtInType);

		// 分析字幕
		Subt subt = new Subt();
		subt.setId(UUID.getUUID());
		subt.setName(analyzer.analysisSubtName());

		scheduleSubject.refreshInfo("正在分析句子");
		subt.setSubtSetences(analyzer.analysisSentens());

		// 分析词库
		scheduleSubject.refreshInfo("正在分析单词");
		Lexicon lexicon = new Lexicon();
		lexicon.setId(UUID.getUUID());
		lexicon.setName(analyzer.analysisSubtName());
		lexicon.setNewWords(analyzer.analysisWords());
		subt.setLexicon(lexicon);
		// 设置词库单词例句
		scheduleSubject.refreshInfo("正在分析单词例句");
		int i = 1;
		for (NewWord newWord : lexicon.getNewWords()) {
			 scheduleSubject.refreshInfo("正在分析单词例句" + i++);
			newWord.setExampleSentStr(subt.getSentencesHtmlWithRedKeyWord(newWord.getSpell()));
		}
		return subt;
	}

	/**
	 * 根据要导入的文件创建句子分析
	 * 
	 * @param file
	 * @return
	 */
	private Analyzer createAnalyzer(final File file, final SubtInType subtInType) {
		if (subtInType.equals(SubtInType.IMPORT_TXT)) {
			return new TxtAnalyzer(file);
		}

		if (subtInType.equals(SubtInType.IMPORT_SRC)) {
			return new SrtAnalyzer(file);
		}
		if (subtInType.equals(SubtInType.REBACK)) {
			return new ReBackAnalyzer(file);
		}
		throw new RuntimeException("不支持的文件导入类型");
	}

	private ScheduleSubject scheduleSubject = ScheduleSubject.getInstance();
}
